package com.augn.service.impl;

import com.augn.entity.Goods;
import com.augn.entity.Result;
import com.augn.entity.User;
import com.augn.mapper.UserMapper;
import com.augn.service.IUserService;
import com.augn.utils.TokenUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author augn
 * @since 2022-09-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper mapper;

    @Autowired
    private TokenUtil tokenUtil;

    //增加一个用户
    @Override
    public Result<?> insertOneUser(User user) {
        int insert = mapper.insert(user);
        if(insert==0){
            return Result.error("-1","新增失败");
        } else{
            return Result.success(insert);
        }
    }

    //根据id删除一个用户
    @Override
    public Result<?> deleteOneUser(Integer id) {
        int delete = mapper.deleteById(id);
        if(delete==0){
            return Result.error("-1","删除失败");
        } else{
            return Result.success(delete);
        }
    }

    //更新一个用户
    @Override
    public Result<?> updateOneUser(User user) {
        int update = mapper.updateById(user);
        if(update==0){
            return Result.error("-1","更新失败");
        } else{
            return Result.success(update);
        }
    }

    //查询一个用户
    @Override
    public Result<?> getOneUser(Integer id) {
        User user = mapper.selectById(id);
        if(user==null){
            return Result.error("-1","无此用户");
        } else{
            return Result.success(user);
        }
    }

    //根据条件查询部分用户
    @Override
    public Result<?> getPartUser(User user,Integer pageNum,Integer pageSize) {
        QueryWrapper<User> wrapper= new QueryWrapper<>();
        if(user.getUserId()!=null){
            wrapper.eq("user_id",user.getUserId());
        }
        if (user.getRole()!=null) {
            wrapper.eq("role",user.getRole());
        }
        if (user.getRoleId()!=null) {
            wrapper.eq("role_id",user.getRoleId());
        }
        if (user.getName()!=null) {
            wrapper.like("name",user.getName());
        }
        if (user.getTelephoneNum()!=null) {
            wrapper.eq("telephone_num",user.getTelephoneNum());
        }

        long pages ,total;
        List<User> userList;
        Map<String,Object> ret = new HashMap<>();
        if((pageNum==null&&pageSize==null)||(pageNum==null&&pageSize!=null)){
            userList =  mapper.selectList(wrapper);
            ret.put("userList",userList);
        } else {
            if(pageSize==null){
                pageSize=20;
            }
            Page<User> userPage = new Page<>(pageNum , pageSize);
            IPage<User> userIPage = mapper.selectPage(userPage , wrapper);
            userList = userIPage.getRecords();
            pages = userIPage.getPages();
            total = userIPage.getTotal();
            ret.put("userList",userList);
            ret.put("pages",pages);
            ret.put("total",total);
        }

        if(userList.isEmpty()){
            return Result.error("-1","没有符合此条件的用户");
        } else{
            return Result.success(ret);
        }
    }

    //查询账号密码是否正确
    @Override
    public Result<?> login(User user) {
        QueryWrapper<User> wrapper= new QueryWrapper<>();
        wrapper.nested(i->i.eq("account",user.getAccount()).eq("password",user.getPassword()))
                .or(i->i.eq("telephone_num",user.getAccount()).eq("password",user.getPassword()));

        Long count = mapper.selectCount(wrapper);
        if(count==1){
            Map<String, Object> map = new HashMap<>();
            User one = mapper.selectOne(wrapper);
            String token = tokenUtil.getToken(user.getAccount(), user.getRole());
            map.put("user",one);
            map.put("token",token);
            return Result.success(map);
        }else {
            return Result.error("-1","账号或密码错误");
        }
    }
}
